package com.itheima.service.imp;

import com.itheima.mapper.MemberMapper;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MemberServiceImp implements MemberService {

    @Autowired
    private  MemberMapper memberMapper;

    public Map<String, Object> findMemberCountByMonth() {
        List<Map<String, Object>> list = memberMapper.findMemberCountByMonth();
        log.info("查询结果：{}",list);
        List<String> months = new ArrayList<>();
        List<String> memberCount = new ArrayList<>();
        for (Map<String, Object> map : list){
            months.add(map.get("months").toString());
            memberCount.add(map.get("memberCount").toString());
        }

        Map<String, Object> data= new HashMap<>();
        data.put("months",months);
        data.put("memberCount",memberCount);
        return data;
    }


    /*
     * 根据日期查询新增会员数量
     */
    public Integer todayNewMember(LocalDate reportDate){
        return memberMapper.todayNewMember(reportDate);
    }
    //本周新增会员数量
    public Integer thisWeekNewMember(LocalDate reportDate){
        //本周第一天
        LocalDate monday = reportDate.with(DayOfWeek.MONDAY);
        reportDate = monday.plusDays(6);
        log.info("本周周一：{}",monday);
        log.info("本周周日：{}",reportDate);
        return memberMapper.thisWeekNewMember(monday,reportDate);
    }

    public Integer thisMonthNewMember(LocalDate reportDate){
        //本月第一天
        LocalDate firstDayOfThisMonth = reportDate.withDayOfMonth(1);
        //本月最后一天
        LocalDate lastDayOfThisMonth = reportDate.withDayOfMonth(reportDate.lengthOfMonth());
        log.info("本月第一天：{}",firstDayOfThisMonth);
        log.info("本月最后一天：{}",lastDayOfThisMonth);
        return memberMapper.thisMonthNewMember(firstDayOfThisMonth,lastDayOfThisMonth);
    }

    //总会员数量
    public Integer totalMember(){
        return memberMapper.totalMember();
    }



}
